'''
Processes data values to find trend for next day
Created on Jul 13, 2014
@author: ANAND
'''
import os
import re
import sqlite3
import downloader
import merge2d

def calc_trend(cur_date, pre_date, dest):
    res = 0
    pro_file = "pro_eb.dat"
    
    dwn_res = downloader.my_downloader(cur_date, pre_date, dest)
    
    if dwn_res[0] == 1:
        cur_file, pre_file = dwn_res[1][0], dwn_res[1][1]
        mrg_file = merge2d.merge(cur_file, pre_file, dest)
        
        mrgfile = open(mrg_file, "r").readlines()
        profile = open(pro_file, "w")
        
        for mline in mrgfile:
            
            mline = mline.rstrip('\n')
            m_line = re.split('\t', mline)
            
            sym_id, my_symbol, my_series, cur_open, cur_high, cur_low, cur_close, cur_pvt, cur_volume, pre_open, pre_high, pre_low, pre_close, pre_pvt = m_line
            
            cur_open, cur_high, cur_low, cur_close, cur_volume, pre_open, pre_high, pre_low, pre_close = float(cur_open), float(cur_high), float(cur_low), float(cur_close), int(cur_volume), float(pre_open), float(pre_high), float(pre_low), float(pre_close)
            
            # 0 = NA, 1 = buy, 2 = sell
            # Normal Trend
            if((cur_open < pre_close) and (cur_open > pre_low) and (cur_low < pre_low) and (cur_close > pre_close)):
                eod_trend = '1'
            elif((cur_open > pre_close) and (cur_open < pre_high) and (cur_high > pre_high) and (cur_close < pre_close)):
                eod_trend = '2'
            else:
                eod_trend = '0'
            
            # Strong Trend
            if((cur_open < pre_low) and (cur_close >= pre_close)):
                strong_trend = '1'
            elif((cur_open < pre_high) and (cur_close <= pre_close)):
                strong_trend = '2'
            else:
                strong_trend = '0'
            
            # Open-High Open-Low trend for today
            if(cur_open == cur_low):
                olh_trend = '1'
            elif(cur_open == cur_high):
                olh_trend = '2'
            else:
                olh_trend = '0'
            
            my_change = cur_close - pre_close
            per_change = round((((cur_close - pre_close)/pre_close)*100),2)
            
            m_line = m_line + [eod_trend, strong_trend, olh_trend, str(my_change), str(per_change)]
            
            nline = "\t".join(m_line)
            nline += "\n"
            profile.writelines(nline)
        
        profile.close()
        res = 1
        os.remove(mrg_file)
        return(res, pro_file)
    
    else:
        print "Enter correct date."
        for i in xrange(len(dwn_res[1])):
            os.remove(dwn_res[1][i])
        return(res, pro_file)

def dump2db(proc_file):
    
    if proc_file[0] == 1:
        my_db = 'eb2day.db'
        os.remove(my_db)
        conn = sqlite3.connect(my_db)
        c = conn.cursor()
        c.execute('''CREATE TABLE stocks
                    (symbol_id text, my_symbol text, my_series text, cur_open real, cur_high real, cur_low real, cur_close real, cur_pvt real, cur_volume integer, 
                    pre_open real, pre_high real, pre_low real, pre_close real, pre_pvt real, eod_trend integer, strong_trend integer, olh_trend integer, my_change real, per_change real)''')
        
        procfile = open(proc_file[1], "r").readlines()
        
        for procline in procfile:
            
            procline = procline.rstrip('\n')
            pr_line = re.split('\t', procline)
            pr_line = tuple(pr_line)
            
            c.execute("INSERT INTO stocks VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", pr_line)
        
        conn.commit()
        conn.close()
        return(1)

def processit(cur_date, pre_date):
    dest = os.getcwd()
    proc = calc_trend(cur_date, pre_date, dest)
    db_res = dump2db(proc)
    if db_res:
        print "Done!"
